package net.zjitc.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.zjitc.common.ResponseResult;
import net.zjitc.entity.Manager;
import net.zjitc.entity.User;
import net.zjitc.service.IManagerService;
import net.zjitc.service.IUserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @auther 郭梦婷
 * @create 2022-05-25 11:37:55
 * @describe 前端控制器
 */
@CrossOrigin
@RestController
@Api(tags = "管理员管理")
public class ManagerController {
    @Reference
    private IManagerService managerService;
    @Reference
    private IUserService userService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    ResponseResult result=new ResponseResult();
    @ApiOperation("获取所有用户信息")
    @GetMapping("manager")
    public ResponseResult getmanagers(@RequestParam(required = false,value ="querystring" ) String querystring,
                                     @RequestParam(required = true ,value = "pagenum") Integer pagenum,
                                     @RequestParam(required = true,value = "pagesize") Integer pagesize){
        Page<Manager> manager=managerService.getManagers(querystring,pagenum,pagesize);
        if (manager.getTotal()>0){
            result.setSuccess("获取管理员成功",manager);
        }else {
            result.setError("获取管理员失败");
        }
        return result;
    }
    @ApiOperation("根据id查找用户")
    @GetMapping("manager/{id}")
    public ResponseResult getmanagerById(@PathVariable("id") Integer id){
        Manager manager = managerService.getById(id);
        if (manager!=null){
            result.setSuccess("获取用户成功",manager);
        }else {
            result.setError("获取用户失败");
        }
        return result;
    }
    @ApiOperation("注册用户")
    @PostMapping("regmanager")
    public ResponseResult Addmanager(@RequestBody Manager manager){
        User user = userService.findByUsername(manager.getMgusername());
        if (user==null) {
            User user1=new User();
            String pwd = passwordEncoder.encode(manager.getMgpassword());
            Date time = new Date();
            manager.setMgpassword(pwd);
            manager.setAddtime(time);
            boolean save = managerService.save(manager);
            Manager manager1= managerService.selectByUsername(manager.getMgusername());

            user1.setUsername(manager1.getMgusername());
            user1.setPassword(pwd);
            user1.setActorid(manager1.getId());
            user1.setRoleid(0);
            user1.setCreateTime(time);
            boolean save1 = userService.save(user1);

            if (save&&save1){
                result.setCreateSuccess("添加用户成功");
            }
        }else {
            result.setError("账号重复");

        }

        return result;

    }



    @ApiOperation("修改管理员")
    @PutMapping("manager/{id}")
    public ResponseResult Updatemanager(@RequestBody Manager manager, @PathVariable("id") int id){
        manager.setId(id);
        boolean update = managerService.updateById(manager);
        if (update){
            result.setSuccess("修改用户成功");
        }else {
            result.setError("修改用户失败");
        }
        return result;
    }

    @ApiOperation("删除用户")
    @DeleteMapping("manager/{id}")
    public ResponseResult Deletemanager(@PathVariable("id") int id){

        int delete= managerService.deleteById(id);
        if (delete!=0){
            result.setSuccess("删除用户成功");
        }else {
            result.setError("删除用户失败");
        }
        return result;
    }

    @ApiOperation("修改管理员密码")
    @GetMapping("/updateManagerPwd")
    public ResponseResult updatePassword(@RequestParam(required = false) String username,
                                         @RequestParam(required = false) String pwd1,
                                         @RequestParam(required = false) String pwd2,
                                         @RequestParam(required = false) String pwd3){
        //加解密方法
        BCryptPasswordEncoder bc = new BCryptPasswordEncoder();
        Manager manager = managerService.selectByUsername(username);
        User user = userService.findByUsername(username);
        //输入密码与数据库密码比较
        boolean matches = bc.matches(pwd1,user.getPassword());
        if (!matches){
            result.setError("旧密码不匹配");
        }else {
            String pwd = passwordEncoder.encode(pwd2);
            user.setPassword(pwd);
            manager.setMgpassword(pwd);
            boolean updateuser = userService.updateById(user);
            boolean updateManger = managerService.updateById(manager);
            if (updateuser&&updateManger){
                result.setSuccess("修改密码成功");
            }else {
                result.setError("修改密码失败");
            }
        }
        return result;
    }

}

